﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Web.Mvc;
using Enter.Mvc.Base.Infrastructure;
using Enter.Services;
using Enter.Common;
using Enter.Mvc.Base.Skinning;
using System.Web.Hosting;
using Enter.Mvc.Base.Service;
using MyChy.Mvc.Base.Service;
using Enter.Common.Helper;

namespace Enter.Mvc.Base
{
    public class ContainerFactory
    {
        private static IUnityContainer current = null;
        private static object lockObject = new object();

        public static IUnityContainer Current
        {
            get
            {
                if (current == null)
                {
                    lock (lockObject)
                    {
                        if (current == null)
                        {
                            lock (lockObject)
                            {
                                current = GetBaseMyUnity();
                            }
                        }
                    }
                }
                return current;
            }
        }

        protected static IUnityContainer GetBaseMyUnity()
        {

            IUnityContainer parentContainer = Register("unity");



            //Controllers控制
            parentContainer.RegisterType<IActionInvoker, UnityControllerActionInvoker>();

			//WebBase
			parentContainer.RegisterType<IUnityBaseService, UnityBaseService>();

           //皮肤
		   parentContainer.RegisterInstance(HostingEnvironment.VirtualPathProvider);
           parentContainer.RegisterType<IViewEngine, UnityViewEngine>();
           parentContainer.RegisterType<ISkinEngine, VirtualPathProviderSkinEngine>();

            return parentContainer;
        }

        private static IUnityContainer Register(string sectionName)
        {
            IUnityContainer container = new UnityContainer();
            try
            {
                container = Unity.Register(sectionName);
                //Unity.Register(sectionName);
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex);
            }
            return container;
        }



    }
}
